using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobUsersSync3.Features.JobUsersSync3
{

    [Guid("47692de3-08ec-4c4c-a6be-ee693969b486")]
    public class JobUsersSync3EventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                // delete preview job item
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (
                           job.Name == "JobUsersSync2_v1"
                        || job.Name == "JobUsersSync2_v2"
                        || job.Name == "JobUsersSync2_v3"
                        || job.Name == "JobUsersSync2_v4"
                        || job.Name == "JobUsersSync2_v5"
                        || job.Name == "JobUsersSync3_v0"
                        || job.Name == "JobUsersSync3_v1"
                        )
                    {
                        job.Delete();
                    }
                }
                JobUsersSync3 listLoggerJob =
                    new JobUsersSync3("JobUsersSync3_v1", site.WebApplication);
                SPDailySchedule schedule = new SPDailySchedule();
                schedule.BeginHour = 1;
                schedule.BeginMinute = 0;
                schedule.BeginSecond = 0;
                schedule.EndHour = 1;
                schedule.EndMinute = 59;
                schedule.EndSecond = 59;

                listLoggerJob.Schedule = schedule;
                listLoggerJob.Update();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (job.Name == "JobUsersSync2_v1"
                        || job.Name == "JobUsersSync2_v2"
                        || job.Name == "JobUsersSync2_v3"
                        || job.Name == "JobUsersSync2_v4"
                        || job.Name == "JobUsersSync2_v5"
                        || job.Name == "JobUsersSync3_v0"
                        || job.Name == "JobUsersSync3_v1"
                        )
                    {
                        job.Delete();
                    }
                }
            }
        }

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
